package com.hsnn.medstgmini.pro.base.service.Impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hsnn.medstgmini.pro.base.dao.PsAnaDruginfoDao;
import com.hsnn.medstgmini.pro.base.service.PsAnaDruginfoManager;
import com.hsnn.medstgmini.util.Pagination;
import com.hsnn.medstgmini.util.ParseDate;
import com.hsnn.medstgmini.util.ParseNumber;

/**
 * @category 统计与查询
 * @author wangbing
 * @date 2015年12月1日10:16:59
 */
@Service
public class PsAnaDruginfoManagerImpl implements PsAnaDruginfoManager {
	@Autowired
	private PsAnaDruginfoDao psAnaDruginfoDao;
	
	/**
	 * @category 按地区统计(月)
	 * @date 2015年12月1日10:18:44
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Pagination getPsMonthAnaByArea(Pagination page) {
		List<Map<String,Object>> list = (List<Map<String, Object>>) psAnaDruginfoDao.getPsMonthAnaByArea(page).getRows();
		for(Map<String,Object> data : list){
			Object delRatio = data.get("delRatio");
			Object date = data.get("date");
			Object retRatio = data.get("retRatio");
			data.put("retRatio", retRatio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(retRatio.toString())*100,6)+"%");
			data.put("date", date==null?"":date.toString().substring(0, 4)+"-"+date.toString().substring(4, 6));
			data.put("delRatio", delRatio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(delRatio.toString())*100,6)+"%");
			data.put("totalAmount", data.get("totalAmount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(data.get("totalAmount").toString()), 3));
			data.put("delAmount", data.get("delAmount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(data.get("delAmount").toString()), 3));
			data.put("retAmount", data.get("retAmount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(data.get("retAmount").toString()), 3));
		}
		
		List<Map<String, Object>> totalList = (List<Map<String, Object>>) psAnaDruginfoDao.getTotalLastMonthByArea(page);
		String yTime = "[";//图表y轴时间
		String total = "[";//采购金额
		String date = ParseDate.parseDateToYm(ParseDate.addMonth(new Date(), -1));
		//循环解析数据
		for(int i =0;i<totalList.size();i++) {
			Map<String, Object> map = totalList.get(i);
			String areaName =  map.get("areaName").toString();
			yTime = yTime +"'"+areaName+ "',";
			total = total + map.get("delRatio") + ",";
		}
		
		if(!"[".equals(yTime)) {//判断是否为空
			yTime = yTime.substring(0, yTime.length()-1) + "]";
			total = total.substring(0, total.length()-1) + "]";
			
			page.getConditions().put("yTime", yTime);
			page.getConditions().put("total", total);
		}
		page.getConditions().put("chartTitle", date+"配送率统计");
		
		return page;
	}
	
	/**
	 * @category 按地区统计(年)
	 * @date 2015年12月1日10:18:44
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Pagination getPsYearAnaByArea(Pagination page) {
		List<Map<String,Object>> list = (List<Map<String, Object>>) psAnaDruginfoDao.getPsYearAnaByArea(page).getRows();
		for(Map<String,Object> data : list){
			Object delRatio = data.get("delRatio");
			Object retRatio = data.get("retRatio");
			data.put("retRatio", retRatio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(retRatio.toString())*100,6)+"%");
			data.put("delRatio", delRatio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(delRatio.toString())*100,6)+"%");
			data.put("totalAmount", data.get("totalAmount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(data.get("totalAmount").toString()), 3));
			data.put("delAmount", data.get("delAmount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(data.get("delAmount").toString()), 3));
			data.put("retAmount", data.get("retAmount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(data.get("retAmount").toString()), 3));
		}
		
		List<Map<String, Object>> totalList = (List<Map<String, Object>>) psAnaDruginfoDao.getTotalLastYearByArea(page);
		String yTime = "[";//图表y轴时间
		String total = "[";//采购金额
		String date = Calendar.getInstance().get(Calendar.YEAR)-1+"";
		//循环解析数据
		for(int i =0;i<totalList.size();i++) {
			Map<String, Object> map = totalList.get(i);
			String areaName =  map.get("areaName").toString();
			yTime = yTime +"'"+areaName+ "',";
			total = total + map.get("delRatio") + ",";
		}
		
		if(!"[".equals(yTime)) {//判断是否为空
			yTime = yTime.substring(0, yTime.length()-1) + "]";
			total = total.substring(0, total.length()-1) + "]";
			
			page.getConditions().put("yTime", yTime);
			page.getConditions().put("total", total);
		}
		page.getConditions().put("chartTitle", date+"配送率统计");
		
		return page;
	}
	
	/**
	 * @category 按地区统计(日)
	 * @date 2015年12月1日10:18:44
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Pagination getPsAnaByArea(Pagination page) {
		List<Map<String,Object>> list = (List<Map<String, Object>>) psAnaDruginfoDao.getPsAnaByArea(page).getRows();
		for(Map<String,Object> data : list){
			Object delRatio = data.get("delRatio");
			Object date = data.get("date");
			Object retRatio = data.get("retRatio");
			data.put("retRatio", retRatio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(retRatio.toString())*100,6)+"%");
			data.put("date", date==null?"":date.toString().substring(0, 4)+"-"+date.toString().substring(4, 6)+"-"+date.toString().substring(6, 8));
			data.put("delRatio", delRatio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(delRatio.toString())*100,6)+"%");
			data.put("totalAmount", data.get("totalAmount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(data.get("totalAmount").toString()), 3));
			data.put("delAmount", data.get("delAmount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(data.get("delAmount").toString()), 3));
			data.put("retAmount", data.get("retAmount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(data.get("retAmount").toString()), 3));
		}
		
		List<Map<String, Object>> totalList = (List<Map<String, Object>>) psAnaDruginfoDao.getTotalYesterdayByArea(page);
		String yTime = "[";//图表y轴时间
		String total = "[";//采购金额
		String date = ParseDate.parseDateToYmd(ParseDate.addDate(new Date(), -1));
		//循环解析数据
		for(int i =0;i<totalList.size();i++) {
			Map<String, Object> map = totalList.get(i);
			String areaName =  map.get("areaName").toString();
			yTime = yTime +"'"+areaName+ "',";
			total = total + map.get("delRatio") + ",";
		}
		
		if(!"[".equals(yTime)) {//判断是否为空
			yTime = yTime.substring(0, yTime.length()-1) + "]";
			total = total.substring(0, total.length()-1) + "]";
			
			page.getConditions().put("yTime", yTime);
			page.getConditions().put("total", total);
		}
		page.getConditions().put("chartTitle", date+"配送率统计");
		
		return page;
	}
	
	/**
	 * @category 按地区统计(季度)
	 * @date 2015年12月1日10:18:44
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Pagination getPsSeaAnaByArea(Pagination page) {
		List<Map<String,Object>> list = (List<Map<String, Object>>) psAnaDruginfoDao.getPsSeaAnaByArea(page).getRows();
		for(Map<String,Object> data : list){
			Object delRatio = data.get("delRatio");
			Object date = data.get("date");
			Object retRatio = data.get("retRatio");
			data.put("retRatio", retRatio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(retRatio.toString())*100,6)+"%");
			data.put("date", date==null?"":date.toString().substring(0, 4)+"年"+date.toString().substring(4, 6)+"季度");
			data.put("delRatio", delRatio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(delRatio.toString())*100,6)+"%");
			data.put("totalAmount", data.get("totalAmount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(data.get("totalAmount").toString()), 3));
			data.put("delAmount", data.get("delAmount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(data.get("delAmount").toString()), 3));
			data.put("retAmount", data.get("retAmount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(data.get("retAmount").toString()), 3));
		}
		
		List<Map<String, Object>> totalList = (List<Map<String, Object>>) psAnaDruginfoDao.getTotalLastSeaByArea(page);
		String yTime = "[";//图表y轴时间
		String total = "[";//采购金额
		String date = ParseDate.getSea(ParseDate.addMonth(new Date(), -3));
		date = date.toString().substring(0, 4)+"年"+date.toString().substring(4, 6)+"季度";
		//循环解析数据
		for(int i =0;i<totalList.size();i++) {
			Map<String, Object> map = totalList.get(i);
			String areaName =  map.get("areaName").toString();
			yTime = yTime +"'"+areaName+ "',";
			total = total + map.get("delRatio") + ",";
		}
		
		if(!"[".equals(yTime)) {//判断是否为空
			yTime = yTime.substring(0, yTime.length()-1) + "]";
			total = total.substring(0, total.length()-1) + "]";
			
			page.getConditions().put("yTime", yTime);
			page.getConditions().put("total", total);
		}
		page.getConditions().put("chartTitle", date+"配送率统计");
		
		return page;
	}
	
	/*-----------------------------------------------按时间查询----------------------------------------------------------*/
	
	/**
	 * @category 按时间统计(月)
	 * @date 2015年12月1日10:18:44
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Pagination getPsMonthAnaByTime(Pagination page) {
		List<Map<String,Object>> list = (List<Map<String, Object>>) psAnaDruginfoDao.getPsMonthAnaByTime(page).getRows();
		for(Map<String,Object> data : list){
			Object delRatio = data.get("delRatio");
			Object date = data.get("date");
			Object retRatio = data.get("retRatio");
			data.put("retRatio", retRatio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(retRatio.toString())*100,6)+"%");
			data.put("date", date==null?"":date.toString().substring(0, 4)+"-"+date.toString().substring(4, 6));
			data.put("delRatio", delRatio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(delRatio.toString())*100,6)+"%");
			data.put("totalAmount", data.get("totalAmount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(data.get("totalAmount").toString()), 3));
			data.put("delAmount", data.get("delAmount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(data.get("delAmount").toString()), 3));
			data.put("retAmount", data.get("retAmount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(data.get("retAmount").toString()), 3));
		}
		
		List<Map<String, Object>> totalList = (List<Map<String, Object>>) psAnaDruginfoDao.getTotalLastMonthByTime(page);
		String yTime = "[";//图表y轴时间
		String total = "[";//采购金额
		//循环解析数据
		for(int i =0;i<totalList.size();i++) {
			Map<String, Object> map = totalList.get(i);
			String date =  map.get("date").toString();
			yTime = yTime +"'"+date.toString().substring(0, 4)+"年"+date.toString().substring(4, 6)+ "月',";
			total = total + map.get("delRatio") + ",";
		}
		
		if(!"[".equals(yTime)) {//判断是否为空
			yTime = yTime.substring(0, yTime.length()-1) + "]";
			total = total.substring(0, total.length()-1) + "]";
			
			page.getConditions().put("yTime", yTime);
			page.getConditions().put("total", total);
		}
		page.getConditions().put("chartTitle", "配送率统计");
		
		return page;
	}
	
	/**
	 * @category 按时间统计(年)
	 * @date 2015年12月1日10:18:44
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Pagination getPsYearAnaByTime(Pagination page) {
		List<Map<String,Object>> list = (List<Map<String, Object>>) psAnaDruginfoDao.getPsYearAnaByTime(page).getRows();
		for(Map<String,Object> data : list){
			Object delRatio = data.get("delRatio");
			Object retRatio = data.get("retRatio");
			data.put("retRatio", retRatio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(retRatio.toString())*100,6)+"%");
			data.put("delRatio", delRatio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(delRatio.toString())*100,6)+"%");
			data.put("totalAmount", data.get("totalAmount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(data.get("totalAmount").toString()), 3));
			data.put("delAmount", data.get("delAmount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(data.get("delAmount").toString()), 3));
			data.put("retAmount", data.get("retAmount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(data.get("retAmount").toString()), 3));
		}
		
		List<Map<String, Object>> totalList = (List<Map<String, Object>>) psAnaDruginfoDao.getTotalLastYearByTime(page);
		String yTime = "[";//图表y轴时间
		String total = "[";//采购金额
		//循环解析数据
		for(int i =0;i<totalList.size();i++) {
			Map<String, Object> map = totalList.get(i);
			String date =  map.get("date").toString();
			yTime = yTime +"'"+date+ "年',";
			total = total + map.get("delRatio") + ",";
		}
		
		if(!"[".equals(yTime)) {//判断是否为空
			yTime = yTime.substring(0, yTime.length()-1) + "]";
			total = total.substring(0, total.length()-1) + "]";
			
			page.getConditions().put("yTime", yTime);
			page.getConditions().put("total", total);
		}
		page.getConditions().put("chartTitle", "配送率统计");
		
		return page;
	}
	
	/**
	 * @category 按时间统计(日)
	 * @date 2015年12月1日10:18:44
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Pagination getPsAnaByTime(Pagination page) {
		List<Map<String,Object>> list = (List<Map<String, Object>>) psAnaDruginfoDao.getPsAnaByTime(page).getRows();
		for(Map<String,Object> data : list){
			Object delRatio = data.get("delRatio");
			Object date = data.get("date");
			Object retRatio = data.get("retRatio");
			data.put("retRatio", retRatio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(retRatio.toString())*100,6)+"%");
			data.put("date", date==null?"":date.toString().substring(0, 4)+"-"+date.toString().substring(4, 6)+"-"+date.toString().substring(6, 8));
			data.put("delRatio", delRatio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(delRatio.toString())*100,6)+"%");
			data.put("totalAmount", data.get("totalAmount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(data.get("totalAmount").toString()), 3));
			data.put("delAmount", data.get("delAmount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(data.get("delAmount").toString()), 3));
			data.put("retAmount", data.get("retAmount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(data.get("retAmount").toString()), 3));
		}
		
		List<Map<String, Object>> totalList = (List<Map<String, Object>>) psAnaDruginfoDao.getTotalYesterdayByTime(page);
		String yTime = "[";//图表y轴时间
		String total = "[";//采购金额
		//循环解析数据
		for(int i =0;i<totalList.size();i++) {
			Map<String, Object> map = totalList.get(i);
			String date =  map.get("date").toString();
			yTime = yTime +"'"+date.toString().substring(0, 4)+"年"+date.toString().substring(4, 6)+"月"+date.toString().substring(6, 8)+ "日',";
			total = total + map.get("delRatio") + ",";
		}
		
		if(!"[".equals(yTime)) {//判断是否为空
			yTime = yTime.substring(0, yTime.length()-1) + "]";
			total = total.substring(0, total.length()-1) + "]";
			
			page.getConditions().put("yTime", yTime);
			page.getConditions().put("total", total);
		}
		page.getConditions().put("chartTitle", "配送率统计");
		
		return page;
	}
	
	/**
	 * @category 按时间统计(季度)
	 * @date 2015年12月1日10:18:44
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Pagination getPsSeaAnaByTime(Pagination page) {
		List<Map<String,Object>> list = (List<Map<String, Object>>) psAnaDruginfoDao.getPsSeaAnaByTime(page).getRows();
		for(Map<String,Object> data : list){
			Object delRatio = data.get("delRatio");
			Object date = data.get("date");
			Object retRatio = data.get("retRatio");
			data.put("retRatio", retRatio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(retRatio.toString())*100,6)+"%");
			data.put("date", date==null?"":date.toString().substring(0, 4)+"年"+date.toString().substring(4, 6)+"季度");
			data.put("delRatio", delRatio==null?"":ParseNumber.toStrDecimal(Double.parseDouble(delRatio.toString())*100,6)+"%");
			data.put("totalAmount", data.get("totalAmount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(data.get("totalAmount").toString()), 3));
			data.put("delAmount", data.get("delAmount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(data.get("delAmount").toString()), 3));
			data.put("retAmount", data.get("retAmount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(data.get("retAmount").toString()), 3));
		}
		
		List<Map<String, Object>> totalList = (List<Map<String, Object>>) psAnaDruginfoDao.getTotalLastSeaByTime(page);
		String yTime = "[";//图表y轴时间
		String total = "[";//采购金额
		//循环解析数据
		for(int i =0;i<totalList.size();i++) {
			Map<String, Object> map = totalList.get(i);
			String date =  map.get("date").toString();
			yTime = yTime +"'"+date.toString().substring(0, 4)+"年"+date.toString().substring(4, 6)+ "季度',";
			total = total + map.get("delRatio") + ",";
		}
		
		if(!"[".equals(yTime)) {//判断是否为空
			yTime = yTime.substring(0, yTime.length()-1) + "]";
			total = total.substring(0, total.length()-1) + "]";
			
			page.getConditions().put("yTime", yTime);
			page.getConditions().put("total", total);
		}
		page.getConditions().put("chartTitle", "配送率统计");
		
		return page;
	}
}
